﻿namespace MefContrib.Models.Provider.Definitions.Fluent
{
    using System;
    using System.Linq;

    public class ExportDecorator : Decorator
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ExportDecorator"/> class, using
        /// the specified <see cref="Provider"/> instance.
        /// </summary>
        /// <param name="provider">The <see cref="FluentDefinitionProvider"/> which is being decorated for an export.</param>
        public ExportDecorator(FluentDefinitionProvider provider)
            : base(provider)
        {
        }

        /// <summary>
        /// Assigns metadata to the current <see cref="ExportDescription"/> which is being defined by the <see cref="Provider"/>.
        /// </summary>
        /// <param name="name">The name of the metadata.</param>
        /// <param name="value">The value of the metadata.</param>
        /// <returns>An <see cref="ExportDecorator"/> object.</returns>
        public virtual ExportDecorator AddMetadata(string name, object value)
        {
            this.CurrentPart.Exports.Last().Metadata.Add(name, value);
            return this;
        }

        /// <summary>
        /// Assigns a contract to the current <see cref="ExportDescription"/>.
        /// </summary>
        /// <param name="contract">A <see cref="String"/> containing the contract.</param>
        /// <returns>An <see cref="ExportDecorator"/> object.</returns>
        public virtual ExportDecorator WithContract(string contract)
        {
            this.CurrentPart.Exports.Last().Contract = contract;
            return this;
        }

        /// <summary>
        /// Assigns a contract to the current <see cref="ExportDescription"/>.
        /// </summary>
        /// <typeparam name="TContract">The <see cref="Type"/> to use as the contract.</typeparam>
        /// <returns>An <see cref="ExportDecorator"/> object.</returns>
        /// <remarks>The <see cref="Type.AssemblyQualifiedName"/> will be used as the contract.</remarks>
        public virtual ExportDecorator WithContract<TContract>()
        {
            string contract =
                typeof(TContract).AssemblyQualifiedName;

            return this.WithContract(contract);
        }
    }
}